package com.itheima.algorithm.sort;

import java.util.ArrayList;
import java.util.Arrays;

// 基数排序
public class RadixSort {

    public static void radixSort(String[] a,int length) {
        // 1.准备桶
        ArrayList<String>[] buckets = new ArrayList[10];
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = new ArrayList<>();
        }
        for( int i = length-1;i >= 0;i-- ){
            for (String s : a){
                buckets[s.charAt(i) - '0'].add(s);
            }

            int k = 0;
            for (ArrayList<String> bucket:buckets){
                 // System.out.println(bucket);
                for (String s:bucket){
                    a[k++] = s;
                }
                bucket.clear();
            }
            System.out.println(Arrays.toString(a));
        }
    }



    public static void main(String[] args) {
        String[] phoneNumbers = new String[6];
        phoneNumbers[0] = "158";
        phoneNumbers[1] = "151";
        phoneNumbers[2] = "235";
        phoneNumbers[3] = "138";
        phoneNumbers[4] = "139";
        phoneNumbers[5] = "159";

        RadixSort.radixSort(phoneNumbers, 3);
//        for (String phoneNumber : phoneNumbers) {
//            System.out.println(phoneNumber);
//        }
    }
}
